import router from "@/routers/index"; // 引入 Vue Router 实例
import { LOGIN_URL } from "@/config"; // 引入登录页 URL
import { ElNotification } from "element-plus"; // 引入 Element Plus 的通知组件
import { useUserStore } from "@/stores/modules/user"; // 引入用户相关的 Vuex store
import { useAuthStore } from "@/stores/modules/auth";
import { RouteRecordRaw } from "vue-router"; // 引入权限相关的 Vuex store

// 引入 views 文件夹下所有 vue 文件
// const modules = import.meta.glob("@/views/**/*.vue");

/**
 * @description 初始化动态路由
 */
export const initDynamicRouter = async () => {
  const userStore = useUserStore(); // 获取用户相关的 Vuex store 实例
  const authStore = useAuthStore(); // 获取权限相关的 Vuex store 实例

  try {
    // 1.获取菜单列表 && 按钮权限列表
    await authStore.getAuthMenuList(); // 获取菜单权限列表
    await authStore.getAuthButtonList(); // 获取按钮权限列表

    // 2.判断当前用户有没有菜单权限
    if (!authStore.authMenuListGet.length) {
      // 如果菜单权限列表为空，则显示无权限通知，并重定向到登录页
      ElNotification({
        title: "无权限访问",
        message: "当前账号无任何菜单权限，请联系系统管理员！",
        type: "warning",
        duration: 3000
      });
      userStore.setToken(""); // 清空用户 token
      router.replace(LOGIN_URL); // 重定向到登录页
      return Promise.reject("No permission"); // 返回权限错误的 Promise
    }

    // 3.添加静态路由
    const staticRoutes: RouteRecordRaw[] = [
      {
        path: "/home/index",
        name: "HomeIndex",
        component: () => import("@/layouts/index.vue"),
        meta: {
          title: "个人主页"
        },
        children: [
          {
            path: "/xiangmuzhanshi/index",
            component: () => import("@/components/XiangMuZhanShi/index.vue"),
            meta: {
              title: "项目展示"
            }
          },
          {
            path: "/chengguozhanshi/index",
            component: () => import("@/components/ChengGuoZhanShi/index.vue"),
            meta: {
              title: "test"
            }
          },
          {
            path: "/pinglunyudianzan/index",
            component: () => import("@/components/PingLunYunDianZan/index.vue"),
            meta: {
              title: "test"
            }
          },
          {
            path: "/xiangmuguanli/index",
            component: () => import("@/components/XiangMuGuanLi/index.vue"),
            meta: {
              title: "test"
            }
          },
          {
            path: "/tiezifabu/index",
            component: () => import("@/components/TieZiFaBu/index.vue"),
            meta: {
              title: "test"
            }
          },
          {
            path: "/tieziliulanpinglun/index",
            component: () => import("@/components/TieZiLiuLanPingLun/index.vue"),
            meta: {
              title: "test"
            }
          },
          {
            path: "/tiezisousuofenlei/index",
            component: () => import("@/components/TieZiSouSuoFenLei/index.vue"),
            meta: {
              title: "test"
            }
          },
          {
            path: "/noncpzhanshi/index",
            component: () => import("@/components/NonCPZhanShi/index.vue"),
            meta: {
              title: "test"
            }
          },
          {
            path: "/dindanguanli/index",
            component: () => import("@/components/DinDanGuanLi/index.vue"),
            meta: {
              title: "test"
            }
          }
        ]
      }
    ];
    // 将静态路由添加到路由器
    staticRoutes.forEach(route => {
      router.addRoute(route);
    });
  } catch (error) {
    // 当按钮 || 菜单请求出错时，重定向到登陆页
    userStore.setToken(""); // 清空用户 token
    router.replace(LOGIN_URL); // 重定向到登录页
    return Promise.reject(error); // 返回错误的 Promise
  }
};
